/*
参数说明:
alertShow-弹出框参数{
  showmsg-服务器弹窗提示
  msg-服务器提示内容
}
scoreData-页面基础数据参数 {
  canPoint-当前可抵扣多少积分
  scale-用来计算积分换算成钱
  point-当前账户积分
  money-根据比率换算出最后可抵扣多少钱
  useScore-input框输入model参数名字
  mianOrderId-订单Id
}
方法：
addressfn-选择收货地址方法跳转
scroeFn-积分抵扣卷选择方法跳转
shopCleanfn-去支付订单方法跳转-(判断条件要不能大于订单可全部抵扣的积分、不能大于账号当前余额)
numberFn-根据比率计算抵扣多少钱
*/
<template>
  <div>
    <alert v-model="alertShow.showmsg" :title="'提示'" @on-show="onShow" @on-hide="onHide">{{alertShow.msg}}</alert>
    <x-header style="background-color:#3d82e5;width:100%;position:absolute;left:0;top:0;z-index:100;" :left-options="{showBack: true}">
      <p name="content" style="margin-left: -12px">积分抵扣</p>
    </x-header>
    <group :title="scoreData.point" style="padding-top: 38px">
        <x-input title="" type="text" placeholder="请输入用于抵扣的积分" keyboard="number" v-model="scoreData.useScore" @on-change="numberFn"></x-input>
    </group>
    <div style="padding-left: 10px">
      <p v-if="isShowTips===true" style="font-size: 14px;color: #fb2323">抵扣积分输入不正确</p>
      <p v-if="isShowTips===false" style="font-size: 14px;color: #aaa9a9">可抵扣￥{{scoreData.money}}</p>
      <flexbox-item :span="9">
        <flexbox title="''" wrap="nowrap" >
          <flexbox-item :span="12" style="font-size: 14px">当前订单可抵扣积分：{{scoreData.canPoint}}&nbsp<a @click="AllmoneyFn" href="javascript:"><p style="color: #3d82e5;display: inline-block">全部抵扣</p></a></flexbox-item>
        </flexbox>
      </flexbox-item>
    </div>
    <div style="padding: 15px;padding-top: 50px">
      <x-button v-if="cashIsDisable===true" disabled action-type="button" style="width: 200px;font-size: 15px;">确定</x-button>
      <x-button v-if="cashIsDisable===false" @click.native="shopCleanfn" style="font-size: 15px;width: 200px;background-color: #3d82e5" type="primary">确定</x-button>
      <div style="width: 100%;text-align: center;padding-top: 25px;">
        <a :href="qqLink"><img src="../../../../assets/kefuButtion.png" width="90px"/></a>
      </div>
    </div>
    <div style="padding: 0 10px;padding-top: 30px">
      <p style="font-size: 12px;color: #636363">积分抵扣规则:</p>
      <p style="font-size: 12px;color: #aaa9a9">1、积分仅限孝行通孝心商城内使用</p>
      <p style="font-size: 12px;color: #aaa9a9">2、在孝心商城购物可以1积分=0.05元抵扣现金（暂不支持服务类商品）</p>
      <p style="font-size: 12px;color: #aaa9a9">3、买家在付款时，每笔订单可使用的积分额度不超过可抵扣的最大值，具体参照商品规定</p>
      <p style="font-size: 12px;color: #aaa9a9">4、使用过积分抵扣的商品，暂不支持退货，如需换货，请联系客服处理</p>
      <p style="font-size: 12px;color: #aaa9a9">5、孝行通掌上养老保留在法律允许范围内对活动的最终解释权</p>
    </div>
  </div>
</template>
<script>
  import { Group, Flexbox, FlexboxItem, XInput, Checklist, XButton, Alert, XHeader } from 'vux'
  import * as urls from '../../../../api/urls'

  export default {
    components: {
      Group,
      FlexboxItem,
      Flexbox,
      XInput,
      Checklist,
      XButton,
      Alert,
      XHeader
    },
    mounted: function () {
      let qq = window.sessionStorage.getItem('sysqq')
      this.qqLink = 'http://wpa.qq.com/msgrd?v=3&uin=' + qq + '&site=qq&menu=yes'
      this.scoreData.mianOrderId = window.sessionStorage.getItem('mianOrderId')
      this.alltotal = window.sessionStorage.getItem('alltotal')
      this.getScore()
      this.weixinfn39()
    },
    data () {
      return {
        alertShow: {
          showmsg: false,
          msg: ''
        },
        isShowTips: false,
        cashIsDisable: true,
        qqLink: '',
        alltotal: '',
        scoreData: {
          canPoint: '',
          scale: '',
          point: '',
          money: '0.00',
          useScore: '',
          mianOrderId: '',
          pointNumber: ''
        }
      }
    },
    methods: {
      weixinfn39 () {
        let that = this
        let userId = window.sessionStorage.getItem('userId')
        let logo = window.sessionStorage.getItem('LOGO')
        let url = window.location.origin
        let link = url + '/api/wxAuth/index?type=t1' + '&uid=' + userId
        that.$wechat.onMenuShareAppMessage({
          title: '孝行通', // 分享标题
          desc: '孝行通·微商城 一个让子女为父母尽孝的老年服务平台', // 分享描述
          link: link, // 分享链接
          imgUrl: logo, // 分享图标
          success: function () {
            // 用户确认分享后执行的回调函数
          },
          cancel: function () {
            // 用户取消分享后执行的回调函数
          }
        })
        that.$wechat.onMenuShareTimeline({
          title: '孝行通', // 分享标题
          link: link, // 分享链接
          imgUrl: logo, // 分享图标
          success: function () {
            // 用户确认分享后执行的回调函数
          },
          cancel: function () {
            // 用户取消分享后执行的回调函数
          }
        })
      },
      getScore () {
        let userId = window.sessionStorage.getItem('userId')
        let that = this
        this.http(urls.usePoint, {userId: userId, orderId: that.scoreData.mianOrderId}, function (res) {
          if (res.status === 100) {
            if (res.data !== null) {
              that.scoreData.canPoint = res.data.canPoint
              that.scoreData.scale = res.data.scale
              if (res.data.point !== null) {
                that.scoreData.pointNumber = res.data.point
                that.scoreData.point = '当前账户积分：' + res.data.point
              }
            }
          } else {
            that.alertShow.showmsg = true
            that.alertShow.msg = res.errMsg
          }
        }, function (res) {
          that.alertShow.showmsg = true
          that.alertShow.msg = res
        })
      },
      numberFn (val) {
        let textmoney = val * 100 * this.scoreData.scale / 100
        if (val !== '') {
          if (!textmoney) {
            this.isShowTips = true
            this.cashIsDisable = true
          } else {
            if (parseInt(val) < 0) {
              this.isShowTips = true
              this.cashIsDisable = true
            } else {
              if (parseInt(val) > this.scoreData.pointNumber) {
                this.isShowTips = true
                this.cashIsDisable = true
              } else {
                this.isShowTips = false
                this.cashIsDisable = false
                this.scoreData.money = parseInt(val) * 100 * this.scoreData.scale / 100
              }
            }
          }
        } else {
          this.isShowTips = false
          this.cashIsDisable = true
          this.scoreData.money = '0.00'
        }
      },
      AllmoneyFn () {
        let pointNumber = this.scoreData.pointNumber
        let canPoint = this.scoreData.canPoint
        if (pointNumber < canPoint) {
          this.scoreData.useScore = pointNumber
        } else {
          this.scoreData.useScore = this.scoreData.canPoint
        }
        this.scoreData.money = parseInt(this.scoreData.canPoint) * 100 * this.scoreData.scale / 100
      },
      shopCleanfn () {
        let that = this
        if (parseInt(this.scoreData.useScore) > parseInt(this.scoreData.canPoint) || parseInt(this.scoreData.useScore) > parseInt(this.scoreData.pointNumber)) {
          that.alertShow.showmsg = true
          that.alertShow.msg = '输入的积分超过限制！'
        } else if (this.alltotal < this.scoreData.money) {
          that.alertShow.showmsg = true
          that.alertShow.msg = '输入的积分抵扣金额大于订单总计！'
        } else {
          window.sessionStorage.setItem('money', that.scoreData.money)
          window.sessionStorage.setItem('useScore', that.scoreData.useScore)
          window.location.href = '/api/wxAuth/go/shoppingCar+shoppingBuy'
        }
      },
      onShow () {
        // 弹窗出现时触发
      },
      onHide () {
        // 弹窗关闭时触发
      }
    }
  }
</script>
<style scoped="scoped"></style>
